﻿using System.Linq;
using System.Security.Cryptography;
using System.Text;

namespace BestEasyCMS.Framework.Security
{
    public static class Md5
    {
        /// <summary>
        /// MD5加密
        /// </summary>
        /// <param name="strValue">要加密的字符串</param>
        /// <returns>加密后的字符串</returns>
        public static string Md5Encrypt(string strValue)
        {
            MD5 md5 = new MD5CryptoServiceProvider();
            var fromData = Encoding.Unicode.GetBytes(strValue);
            var targetData = md5.ComputeHash(fromData);

            return targetData.Aggregate<byte, string>(null, (current, t) => current + t.ToString("x")).ToUpper(); 
        }

        /// <summary>
        /// MD5加密
        /// </summary>
        /// <param name="strValue">要加密的字符串</param>
        /// <returns>加密后的字符串</returns>
        public static string ToMd5Encrypt(this string strValue)
        {
            MD5 md5 = new MD5CryptoServiceProvider();
            var fromData = Encoding.Unicode.GetBytes(strValue);
            var targetData = md5.ComputeHash(fromData);

            return targetData.Aggregate<byte, string>(null, (current, t) => current + t.ToString("x")).ToUpper();
        }
    }
}
